import gameConfig from "../../engines/configs/gameConfig";
import { services, modules, managers } from "../../engines/gameEngine";
import node_component from "../../engines/libs/node_component";
import { GAME_MODE } from "../../engines/managers/gameContent/gameMode_json";
import { REWARD_TYPE } from "../../engines/services/userContent/userInterface";
import { MODE_NAME_MAP } from "../Const";
import { PAGE } from "../Enum";
import { IImgPair } from "../Interface";
import mainRoot from "../MainRoot";

// const Mode = cc.Enum({
//     NORMAL: 0,
//     MATCHEQUATION: 1,// 移火柴模式
//     CHANGEONEMODE: 2,// 改一笔变字
//     CHARPUZZLE: 3,// 拼字
//     DRAW_LINE_PROTECTED: 4,// 画线保护小人
//     ONE_DRAW: 5,// 一笔画完
//     RED_BLOCK: 6,// 解锁红木
//     DRAG_BLOCK: 7,// 抽积木
//     CORE_BALL: 8,
//     PUSH_BOX: 9,//推箱子
//     GUSS_PLACE: 10,
//     SUROUND: 11,
//     PINZI: 12,
//     FIGHTMODE: 13,
//     FINDWORD: 14,
//     FANTASTIC: 16,//奇思妙想
//     IQ_COMPUTE: 17,//细节计算
//     CONNECT: 18,//连线挑战

//     WONDERFUL: 100,
//     WONDERFUL2: 101
// });

const Mode = cc.Enum(GAME_MODE);
const { ccclass, property, menu, disallowMultiple } = cc._decorator;

@ccclass
@menu("工具组件/模式按钮")
export default class ModeBtn extends cc.Component {
  @property({ displayName: "是否为在外面展示的按钮" })
  private isOutBtn: boolean = false;
  @property({ type: Mode, displayName: "对应的模式" })
  private m_mode: number = Mode.NORMAL;
  @property({ type: cc.Sprite, displayName: "模式内容展示的图标" })
  private contentImg: cc.Sprite = null;
  @property({ type: cc.Sprite, displayName: "模式标题的图标" })
  private titleImg: cc.Sprite = null;
  @property({ type: cc.Label, displayName: "广告进度" })
  private AdProgress: cc.Label = null;
  @property({ type: cc.Node, displayName: "广告图标" })
  private AdIcon: cc.Node = null;
  @property({ type: cc.Label, displayName: "关卡进度" })
  private levelProgress: cc.Label = null;
  @property({ displayName: "是否自动对齐内容图标" })
  private autoWidge: boolean = false;

  BtnInit(mode: GAME_MODE) {
    this.m_mode = mode;
  }

  async onLoad() {
    let imgPair: IImgPair = mainRoot.ConfigSerive.ModeImgMap[this.m_mode];
    try {
      node_component.loadResourcesPicture(
        `Img/Mode/${imgPair.titleImg}${this.isOutBtn ? "O" : ""}`,
        this.titleImg
      );
      node_component
        .loadResourcesPicture(
          `Img/Mode/${imgPair.contentImg}${this.isOutBtn ? "O" : ""}`,
          this.contentImg
        )
        .then((spriteF) => {
          if (this.autoWidge) {
            this.contentImg.node.y =
              (spriteF.getOriginalSize().height - this.node.height) * 0.5;
          }
        });
    } catch (e) {}
    this.RefreshInfo();
  }

  OnEnterBtn(event, custom: string) {
    managers().soundMgr.playSound("mainSound/anNiu");
    const type = Number(custom);
    const mode = this.m_mode;

    // console.log("包名");

    let completeFun = type
      ? async () => {
        console.info("bundleName",mode)
        console.warn('重置3');
          mainRoot.GotTips = false;
          mainRoot.GotAnswer = false;
          let bundleName: string =
            mainRoot.LevelSystem.GetSignleLevelInfo(mode).bundleName;
          // services().userSrv.setPower(-gameConfig.MODE_POWER[mode], true, true);

          await mainRoot.LevelSystem.LoadLevel(bundleName, mode);
          mainRoot.UISystem.ClosePage(PAGE.MODEMENU);
        }
      : () => {
          let levelMenuPrams = {
            mode: mode,
          };
          // if(mode == Mode.BHMB||mode == Mode.CJTT){
          //   mainRoot.UISystem.OpenPage(PAGE.LEVELSKINMENU, levelMenuPrams);
          // }else{
            mainRoot.UISystem.OpenPage(PAGE.LEVELMENU, levelMenuPrams);
          // }


        };

    if (!services().userSrv.getModeUnlocked(this.m_mode)) { //如果没解锁
      services()
        .miniGameSrv.getAppApiInstance()
        .loadAndShowVideoAd(
          () => {
            services().userSrv.addModeAdCount(this.m_mode);
          },
          (e) => {
            console.error(e);
          },
          () => {
            if (
              services().userSrv.getModeAdCount(this.m_mode) >=
              gameConfig.MODE_VIDEO_COUNT[this.m_mode]
            ) {
              services().userSrv.UnlockMode(this.m_mode);
              completeFun();
            }
            this.RefreshInfo();
          },
          `看广告解锁${MODE_NAME_MAP[this.m_mode]}`
        );
      return;
    }
    if (services().userSrv.getPower() - gameConfig.MODE_POWER[mode] < 0) {
      modules().uiControlModule.showUserPropGet(
        REWARD_TYPE.POWER,
        gameConfig.ADD_ONCE_POWER
      );
      return;
    }
    services().userSrv.setPower(-gameConfig.MODE_POWER[mode], true, true);
    completeFun();
  }

  RefreshInfo() {
    // console.log("刷新模式按钮",this.m_mode,services().userSrv.getModeAdCount(this.m_mode));
    this.AdProgress.node.active = !services().userSrv.getModeUnlocked(
      this.m_mode
    );
    this.AdProgress.string = `${services().userSrv.getModeAdCount(
      this.m_mode
    )}:${gameConfig.MODE_VIDEO_COUNT[this.m_mode]}`;
    this.AdIcon.active = !services().userSrv.getModeUnlocked(this.m_mode);
    if (this.levelProgress) {
      this.levelProgress.string = `${mainRoot.LevelSystem.GetFarLevel(
        this.m_mode
      )}:${mainRoot.LevelSystem.GetMaxLevel(this.m_mode)}`;
    }
  }
}
